<template>
  <div @keyup.enter="getData()">
    <input type="text" style="display: none" />
    <el-form class="query-form" :inline="true" :model="params">
      <el-form-item label="名称查询">
        <el-input
          v-model="params.load_name"
          placeholder="产品名称查询"
          @keyup.enter="getData()"
        ></el-input>
      </el-form-item>
    </el-form>

    <el-table border :data="tableData" v-loading="loading">
      <el-table-column prop="load_name" align="center" label="产品名称"></el-table-column>
      <el-table-column
        prop="p"
        align="center"
        :label="'项目数(' + totalFunds('p') + ')'"
      ></el-table-column>
      <el-table-column
        prop="p1"
        align="center"
        :label="'累计金额(' + totalFunds('p1') + ')(万元)'"
      ></el-table-column>
      <el-table-column
        prop="p2"
        align="center"
        :label="'在保金额(' + totalFunds('p2') + ')(万元)'"
      ></el-table-column>
      <el-table-column prop="p3" align="center" :label="'代偿金额(' + totalFunds('p3') + ')(万元)'">
        <template v-slot="scope">
          {{ scope.row.p3 || '-' }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { addressSplit, addressCut } from '@vimi/utils-tool'

import { provinceGoodsServiceId } from '@/credit/views/province/goods/goodsMixins'

export default {
  components: {},
  props: {},
  data() {
    return {
      tableName: 'industrial_package_total_view',
      params: {
        load_name: '',
        fields: ['industrial_package_id', 'project_num', 'left_funds_total', 'load_name'],
      },
      loading: false,
      tableData: [],
    }
  },
  computed: {
    ...mapGetters(['clientId', 'user']),
    orgRegionLever() {
      return this.user.orgRegionLever
    },
    // totalFunds() {
    //   const total = this.tableData.reduce((prev, curr) => {
    //     return prev + (+curr.reply_funds_total || 0)
    //   }, 0)
    //   return Math.round(total * 1e2) / 1e2
    // }
  },
  watch: {},
  created() {
    this.setParams()
    this.getData()
  },
  mounted() {},
  methods: {
    totalFunds(key) {
      if (!key) {
        return
      }
      const total = this.tableData.reduce((prev, curr) => {
        return prev + (+curr[key] || 0)
      }, 0)
      return Math.round(total * 1e2) / 1e2
    },
    setParams() {
      this.params['orWhere.db_service_id.eq'] = this.clientId
      this.params['orWhere.db_service_id.is'] = 'null'
      this.params.groupFunc = `sum(${this.tableName}.project_num),sum(${this.tableName}.reply_funds_total),sum(${this.tableName}.left_funds_total)`
      this.params.groupFields = ['industrial_package_id']
      if (this.orgRegionLever === 1) {
        this.params['service_id.eq'] = provinceGoodsServiceId
      } else {
        // 市担保可以查看省担保的产品（适用区域包括市，审批通过，只能查看不能修改）
        const queryAddress = addressSplit(this.user.queryAddressCode)
        const { length } = queryAddress
        if (length) {
          queryAddress[length - 1] = addressCut(queryAddress[length - 1])
        }
        this.params['orWhere.supply_address_code.like'] = queryAddress

        this.params.orWhereAndTemplate = 1
        this.params['orWhereAnd.service_id.eq'] = this.clientId
        this.params['orWhereAnd.service_id.neq'] = provinceGoodsServiceId

        this.params['orWhereAnd.service_id.in'] = provinceGoodsServiceId
        this.params['orWhereAnd.is_approval.eq'] = 1
      }
    },
    async getData() {
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData('credit', this.tableName, this.params)
      )
      this.loading = false
      this.tableData = (res && res.data) || []
    },
  },
}
</script>
<style lang="scss"></style>
